package com.jltx.xq.simplewatch.db.helper;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

import com.jltx.xq.simplewatch.base.App;
import com.jltx.xq.simplewatch.db.SimpleWatchContract;
import com.jltx.xq.simplewatch.news.model.bean.NewsRecord;
import com.jltx.xq.simplewatch.utils.IOUtils;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/08/26.
 */

public class NewsRecordDBHelper {
    private static  Uri newRecordUri = SimpleWatchContract.CONTENT_NEWS_RECORD_URI;
    private static final String MAX_PAGE = "max_page";
    /**
     * 获取新闻数据库page为最大的一页
     * @return
     */
    public static NewsRecord getLastNewsRecord(String channel) {
        String[] proj = new String[]{
                SimpleWatchContract.NewsRecordColumns.ID
                , SimpleWatchContract.NewsRecordColumns.CHANNEL_CODE
                , SimpleWatchContract.NewsRecordColumns.TIME
                , SimpleWatchContract.NewsRecordColumns.PAGE
                , SimpleWatchContract.NewsRecordColumns.DATA
                , "MAX(" + SimpleWatchContract.NewsRecordColumns.PAGE + ") AS "+MAX_PAGE};
        //查询条件
        String selection = SimpleWatchContract.NewsRecordColumns.CHANNEL_CODE + " =? ";
        String[] selectionArgs = new String[]{channel};
        //排序
        String orderBy = SimpleWatchContract.NewsRecordColumns.PAGE + " DESC";
        Cursor cursor = null;
        NewsRecord result = null;
        try {
            cursor = App.getApp().getContentResolver().query(newRecordUri, proj, selection, selectionArgs, orderBy);
            if (cursor != null && cursor.moveToNext()) {
                result = new NewsRecord();
                result.setChannel(cursor.getString(cursor.getColumnIndex(SimpleWatchContract.NewsRecordColumns.CHANNEL_CODE)));
                result.setPage(cursor.getInt(cursor.getColumnIndex(SimpleWatchContract.NewsRecordColumns.PAGE)));
                result.setTime(cursor.getInt(cursor.getColumnIndex(SimpleWatchContract.NewsRecordColumns.TIME)));
                result.setData(cursor.getString(cursor.getColumnIndex(SimpleWatchContract.NewsRecordColumns.DATA)));
            }
        } finally {
            IOUtils.closeIO(cursor);
        }
        return result;
    }

    /**
     * 通过传入的页码获取对应上一页的数据
     * @param channel
     * @param page
     * @return
     */
    public static NewsRecord getNewsRecord(String channel,int page) {
        String[] proj = new String[]{
                SimpleWatchContract.NewsRecordColumns.ID
                , SimpleWatchContract.NewsRecordColumns.CHANNEL_CODE
                , SimpleWatchContract.NewsRecordColumns.TIME
                , SimpleWatchContract.NewsRecordColumns.PAGE
                , SimpleWatchContract.NewsRecordColumns.DATA};
        //查询条件
        String selection = SimpleWatchContract.NewsRecordColumns.CHANNEL_CODE + " =? AND "+ SimpleWatchContract.NewsRecordColumns.PAGE + " =? ";
        String[] selectionArgs = new String[]{channel,page+""};
        //排序
        String orderBy = SimpleWatchContract.NewsRecordColumns.PAGE + " DESC";
        Cursor cursor = null;
        NewsRecord result = null;
        try {
            cursor = App.getApp().getContentResolver().query(newRecordUri, proj, selection, selectionArgs, orderBy);
            if (cursor != null && cursor.moveToNext()) {
                result = new NewsRecord();
                result.setChannel(cursor.getString(cursor.getColumnIndex(SimpleWatchContract.NewsRecordColumns.CHANNEL_CODE)));
                result.setPage(cursor.getInt(cursor.getColumnIndex(SimpleWatchContract.NewsRecordColumns.PAGE)));
                result.setTime(cursor.getInt(cursor.getColumnIndex(SimpleWatchContract.NewsRecordColumns.TIME)));
                result.setData(cursor.getString(cursor.getColumnIndex(SimpleWatchContract.NewsRecordColumns.DATA)));
            }
        } finally {
            IOUtils.closeIO(cursor);
        }
        return result;
    }

    /**
     *
     * @param channel
     * @param page
     * @return
     */
    public static NewsRecord getNewsRecordNoNull(String channel,int page) {
        NewsRecord newsRecord = getNewsRecord(channel,page);
        return newsRecord != null ? newsRecord : new NewsRecord();
    }

    /**
     *
     * @param channel
     * @return
     */
    public static NewsRecord getLastNewsRecordNoNull(String channel) {
        NewsRecord newsRecord = getLastNewsRecord(channel);
        return newsRecord != null ? newsRecord : new NewsRecord();
    }

    /**
     * 保存一条最新的微信精选数据新闻数据
     * @param channel
     * @param page
     * @param time
     * @param data
     */
    public static void saveNews(String channel,int page,int time,String data){
        ContentValues cv = new ContentValues();
        cv.put(SimpleWatchContract.NewsRecordColumns.CHANNEL_CODE,channel);
        cv.put(SimpleWatchContract.NewsRecordColumns.PAGE,page);
        cv.put(SimpleWatchContract.NewsRecordColumns.TIME,time);
        cv.put(SimpleWatchContract.NewsRecordColumns.DATA, data);
        App.getApp().getContentResolver().insert(newRecordUri,cv);
    }
}
